// OSSD project 
// extension that uses a new Chrome API and the google app engine
// bjarkik12 magnusk12

function httpGet(theUrl)
{
	var xmlHttp = null;

	xmlHttp = new XMLHttpRequest();
	// "false" makes this call synchronous
	xmlHttp.open( "GET", theUrl, false ); 
	xmlHttp.send( null );
	return xmlHttp.responseText;
}

chrome.apitime.doGet (function(p) {
  // Chrome API time
  var apiTimeOrg = p[0].doubleTime;
  var apiTimeapi = new Date(apiTimeOrg * 1000);
  var apiTime = "Chrome API time is : " + apiTimeapi.toUTCString();
  document.getElementById("apitime").innerHTML = apiTime;
  
  // Google app time
  var myDateAppOrg = httpGet("http://ossd-time.appspot.com/");
  var myDateApp = new Date( httpGet("http://ossd-time.appspot.com/") * 1000);
  var apptime = "Google App time is : " + myDateApp.toUTCString();
  document.getElementById("apptime").innerHTML = apptime;
  
  // Time difference
  var difTimeOrg = Math.abs(myDateAppOrg - p[0].doubleTime);
  if (difTimeOrg < 60) {
    var diftime = "Time difference in seconds : " + difTimeOrg;
    document.getElementById("diftime").innerHTML = diftime;   
  } 
  else if (difTimeOrg < 3600) {
    var diftime = "Time difference in minutes and seconds : " + parseInt(difTimeOrg/60,10) + ":" + difTimeOrg%60;
    document.getElementById("diftime").innerHTML = diftime;     
  }
  else {
    var diftime = "Time difference in hours, minutes and seconds : " 
	+ parseInt(difTimeOrg/3600,10) + ":" + parseInt(difTimeOrg%3600/60,10) + ":" + parseInt(difTimeOrg%3600%60,10);
    document.getElementById("diftime").innerHTML = diftime;  
  }
} );
